package com.pocketmenu.ui;


import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class ShowLocationMap extends MapActivity  {

	private MapView mapView;
	private MyLocationOverlay myLocationOverlay;
			
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // main.xml contains a MapView
        setContentView(R.layout.activity_show_location_map); 
        
        // extract MapView from layout
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		
		// create an overlay that shows our current location
		myLocationOverlay = new MyLocationOverlay(this, mapView);
				
		// add this overlay to the MapView and refresh it
		mapView.getOverlays().add(myLocationOverlay);
		mapView.postInvalidate();
		
		myLocationOverlay.runOnFirstFix(new Runnable() {
			
			@Override
			public void run() {
				zoomToMyLocation();
				
			}
		});

    }
    
    @Override
	protected void onResume() {
		super.onResume();		
		myLocationOverlay.enableMyLocation();
	}

	@Override
	protected void onPause() {
		super.onPause();
		myLocationOverlay.disableMyLocation();
	}
	
	/**
	 * This method zooms to the user's location with a zoom level of 10.
	 */
	private void zoomToMyLocation() {
		GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();		
		
		if(myLocationGeoPoint != null) {
			mapView.getController().animateTo(myLocationGeoPoint);
			mapView.getController().setZoom(18);			
			Log.i("Map", "Location Ok, Lat: " + (myLocationGeoPoint.getLatitudeE6() / 1E6) + "Lon: " + (myLocationGeoPoint.getLongitudeE6()/1E6));
		}
		else {			
			Log.i("Map", "No Entro");
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	public void btnMyLocation_Click(View view)
	{
		zoomToMyLocation();
	}

}
